<template>
    <div class="widgets bg-white">
        <el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
            <el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" v-if="!isAdd" disabled>新增</el-button>
            <el-button type="primary" v-else @click="add">新增</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
            <el-button type="danger" v-else @click="del">删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
            <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
            <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
            <el-button-group class="btn-other">
                <el-button v-if="multipleSelection.length <= 0" disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导出" v-else @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <!-- <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url + 'client/importClient?systemId=' + systemId" :show-file-list="false" multiple style="display: inline-block;" :on-success="importExcelSuccess">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button>     -->             
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            searchText: '',
            row: this.$store.state.customerStore.row,
            multipleSelection: this.$store.state.customerStore.multipleSelection,
            selectRowId: this.$store.state.customerStore.selectRowId,
            isAdd: true
        }
    },
    watch: {
        row(newVal) {
            newVal[0].hasOwnProperty('clients') ? this.isAdd = true : this.isAdd = false;
        },
        searchText(newVal) {
            if(!newVal) {
                this.$store.dispatch('customerStore/getTreeData');      //刷新数据
            }
        }
    },
    methods: {
        search() {
            if(this.searchText) {
                let data = this.$store.state.customerStore.searchResult,
                    newArr = [];
                data.forEach(item => {
                    if((item['name'] && item['name'].indexOf(this.searchText) > 0) || (item['toNamePinyin'] && item['toNamePinyin'].indexOf(this.searchText) > 0) || (item['toSimplePinyin'] && item['toSimplePinyin'].indexOf(this.searchText) > 0) || (item['address'] && item['address'].indexOf(this.searchText) > 0)) {
                        newArr.push(item);
                    }
                });
                this.$store.commit('customerStore/updateTableData2', newArr);
            }
        },
        refresh() {
            const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
        },
        add() {         //新增
            this.$store.commit('customerStore/updateRowId');
			this.$util.openLayerIframe({title: '客户资料-新增', zIndex: 13, width: '550px', height: '80%', url: this.$domain + '/#/basicData/customer/form'});
        },
        del() {     //删除
            this.$confirm('确定删除选中数据?', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var type = {
                    headers: {
                        'Content-type': 'application/json;charset=UTF-8'
                    }
                }
                this.$http.post('client/deleteClient', JSON.stringify(this.selectRowId), type).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({message: '删除成功', type: 'success', duration: 1500});
                        this.$store.commit('customerStore/updateExpandNodeId', this.multipleSelection[0]['clientCategoryId']);     //根据客户大类来展开节点
                        setTimeout(() => {
                           this.$store.dispatch('customerStore/getTreeData');
                        }, 1000);
                    } else {
                        this.$message.error(res.data.msg || '删除失败');
                    }
                });
            }).catch(() => {
                this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
        },
        disabedBtn(status, msg) {       //禁用、启用
            var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('client/startOrDisableClient?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
                    setTimeout(() => {
                        this.$store.dispatch('customerStore/getTreeData');
                    }, 1000);
                } else {
                    this.$message.error(this.data.msg || '禁用失败');
                }
            });
        },
        exportExcel() {     //导出
            var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('client/exportClient',{params: { idList: idSting }}).then(res => {
                this.$message({message: '导出成功', type: 'success', duration: 1500});
                window.location.href = this.$url + 'client/exportClient?idList=' + idSting;          //导出时触发
            }).catch(err => {
                this.$message.error(err);
            });
        },
        // importExcelSuccess(res) {          //导入
        //     if(res.code === 20001) {
        //         this.$message({message: '导入成功', type: 'success', duration: 1500});
        //         setTimeout(() => {
        //             this.$store.dispatch('customerStore/getTreeData');
        //         }, 1000);
        //     }else{
        //         this.$message.error(res.msg || '导入失败');
        //     }
        // }
    },
}
</script>